package 链表;

public class MysingleList implements IList{

   // public ListNode last;  //链表的尾节点
    public ListNode head;   //链表的头节点-->链表的属性
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    @Override
    public void addLast(int data) {
        //使用尾插法时要注意判断链表是否为空
        ListNode node = new ListNode(data);

        if(head==null){
            head = node;
        }else {
            ListNode cur = head;
            while (cur.next!=null){//while(cur!=null)
                cur = cur.next;
            }
            cur.next = node;//cur=node;
        }


    }

    @Override
    public void addIndex(int index, int data) throws IndexException{
        //cur节点如果已经指向后一个节点,那么是无法指向前一个节点的

        //1.错误解法
        /*ListNode node = new ListNode(data);
        int flag = 0;
        ListNode cur = head;
        while(cur!=null){
            flag++;
            cur=cur.next;
        }
        if(index>flag){
            System.out.println("超出数组范围!");
        }else{
            for(int i=0;i<flag;i++){
                if(i==index){
                    node.next = cur.next;
                    cur.next = node;
                }
            }
        }*/

        //2.正确解法
        if(index<0||index>size()){
            throw new IndexException("index不合法"+index);
        }
        ListNode node = new ListNode(data);
        if(head==null){
            head=node;
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){//index是要插入的下标位置  size是整个链表的长度
            addLast(data);
            return;
        }
        //中间插入
        ListNode cur = searchPrevIndex(index);//返回index前一个目标节点
        node.next=cur.next;
        cur.next=node;
    }

    private ListNode searchPrevIndex(int index){
        ListNode cur=head;
        int count=0;
        while(count!=index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.value==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while (cur!=null){
            if(cur.value==key){
                cur.next=cur.next.next;
                break;
            }
            cur =cur.next;
        }
    }

    @Override
    public void removeAIIKey(int key) {
        ListNode cur = head;
        while (cur!=null){
            if(cur.value==key){
                cur.next=cur.next.next;
            }
            cur =cur.next;
        }
    }

    @Override
    public int size() {
        int i=0;
        ListNode cur = head;
        while(cur!=null){
            i++;
            cur = cur.next;
        }
        return i;
    }

    @Override
    public void clear() {
        while(head!=null) {
            System.out.println(head.value + " ");
            head = head.next;
        }
    }

    @Override
    public void display() {
        ListNode cur = head;
    while(cur!=null){
        System.out.print(cur.value+" ");
        cur = cur.next;
    }
    }

    //创建一个链表
    static class ListNode{
        public int value;
        public ListNode next;//让next的类型为ListNode
        public ListNode prev;
        public ListNode(int value){//构造方法
            this.value =value;
        }
    }//每一个节点为一个内部类


}
